# NOTE: There are some implicit "requires" in these macros, so you
# should start your SLS with:
#
# include:
#   - kubectl-config

#####################################################################

# a macro for running "kubectl" with retries, the "kubeconfig" and the
# right dependencies...

{% macro _kubectl_run(args) -%}
  caasp_cmd.run:
    - name: kubectl --request-timeout=1m --kubeconfig={{ pillar['paths']['kubeconfig'] }} {{ args }}
    - retry:
        attempts: 5
        interval: 1
    - require:
      - {{ pillar['paths']['kubeconfig'] }}
{%- if 'require' in kwargs %}
  {%- for r in kwargs['require'] %}
      - {{ r }}
  {%- endfor %}
{%- endif %}
{%- if 'watch' in kwargs %}
    - watch:
  {%- for w in kwargs['watch'] %}
      - {{ w }}
  {%- endfor %}
{%- endif %}
{%- if 'unless' in kwargs %}
    - unless:
       - {{ kwargs['unless'] }}
{%- endif %}
{%- if 'check_cmd' in kwargs %}
    - check_cmd:
       - {{ kwargs['check_cmd'] }}
{%- endif %}
{%- if 'onlyif' in kwargs %}
    - onlyif:
       - {{ kwargs['onlyif'] }}
{%- endif %}
{%- endmacro %}

#####################################################################

{% macro kubectl(name, args) -%}
{{ name }}:
  {{ _kubectl_run(args, **kwargs) }}
{%- endmacro %}

#####################################################################

{% macro kubectl_apply(manifest) -%}
{{ manifest }}:
  {{ _kubectl_run("apply -f " + manifest, **kwargs) }}
{%- endmacro %}

#####################################################################

{% macro kubectl_apply_template(src, manifest) -%}
{{ manifest }}:
  file.managed:
    - name:        {{ manifest }}
    - source:      {{ src }}
    - template:    jinja
  {{ _kubectl_run("apply -f " + manifest,
                  watch=["file: " + manifest] + kwargs.pop('watch', []),
                  **kwargs) }}
{%- endmacro %}

{% macro kubectl_apply_file(src, manifest) -%}
{{ manifest }}:
  file.managed:
    - name:        {{ manifest }}
    - source:      {{ src }}
  {{ _kubectl_run("apply -f " + manifest,
                  watch=["file: " + manifest] + kwargs.pop('watch', []),
                  **kwargs) }}
{%- endmacro %}

{% macro kubectl_apply_dir_template(src, dest) -%}
{{ dest }}:
  file.recurse:
    - name:        {{ dest }}
    - source:      {{ src }}
    - user:        root
    - group:       root
    - file_mode:   0600
    - dir_mode:    0700
    - clean:       True
    - template:    jinja
  {{ _kubectl_run("apply -f " + dest,
                  watch=["file: " + dest] + kwargs.pop('watch', []),
                  **kwargs) }}
{%- endmacro %}

#####################################################################

{% macro kubectl_wait_for_deployment(deployment, namespace = 'kube-system', timeout = 600) -%}
wait-for-{{ deployment }}-deployment:
  caasp_cmd.run:
    - name: |-
        desiredReplicas=$(kubectl --request-timeout=1m --kubeconfig={{ pillar['paths']['kubeconfig'] }} get deployment {{ deployment }} --namespace={{ namespace }} --template {{ '{{.spec.replicas}}' }})
        readyReplicas=$(kubectl --request-timeout=1m --kubeconfig={{ pillar['paths']['kubeconfig'] }} get deployment {{ deployment }} --namespace={{ namespace }} --template {{ '{{.status.readyReplicas}}' }})
        availableReplicas=$(kubectl --request-timeout=1m --kubeconfig={{ pillar['paths']['kubeconfig'] }} get deployment {{ deployment }} --namespace={{ namespace }} --template {{ '{{.status.availableReplicas}}' }})
        updatedReplicas=$(kubectl --request-timeout=1m --kubeconfig={{ pillar['paths']['kubeconfig'] }} get deployment {{ deployment }} --namespace={{ namespace }} --template {{ '{{.status.updatedReplicas}}' }})
        [ "$readyReplicas" == "$desiredReplicas" ] && [ "$availableReplicas" == "$desiredReplicas" ] && [ "$updatedReplicas" == "$desiredReplicas" ]
    - timeout: {{ timeout }}
    - retry:
        attempts: {{ timeout }}
        interval: 1
{%- endmacro %}